package com.adams.audit.config.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adams.access.db.bean.AuditStep;
import com.adams.audit.common.AuditConstants;
import com.adams.audit.config.dao.IAuditStepDao;
import com.adams.audit.config.service.IAuditStepService;
import com.adams.audit.web.bean.AuditType;

/**
 * 
 * 
 * File Name : AuditStepServiceImpl.java
 * 
 * @Description : 审核步骤service实现
 */
@Service("auditStepService")
@Transactional
public class AuditStepServiceImpl implements IAuditStepService
{

	@Autowired
	private IAuditStepDao auditStepDao;

	// private final Logger logger =
	// Logger.getLogger(AuditStepServiceImpl.class);

	@Override
	public List<String> createStepNo(int step)
	{
		return this.productSteps(step + "");
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<AuditStep> listAllStepsByType(String type)
	{
		String hql = "from AuditStep where typeCode='" + type
				+ "' order by step asc";
		List<AuditStep> list = null;
		list = auditStepDao.query(hql);
		return list;
	}

	private List<String> productSteps(String maxStep)
	{
		List<String> list = null;
		if (maxStep != null && !"".equals(maxStep))
		{
			list = new ArrayList<String>();
			for (int i = 1; i <= Integer.parseInt(maxStep); i++)
			{
				list.add(i + "");
			}
		}
		return list;
	}

	@Override
	@Transactional
	public boolean saveAuditStepForType(List<AuditStep> auditStepList)
	{
		if (auditStepList.isEmpty())
			return false;
		String typeCode = ((AuditStep) auditStepList.get(0)).getTypeCode();
		auditStepDao.executeUpdate("delete from AuditStep a where a.typeCode='"
				+ typeCode + "'");
		for (AuditStep auditStep : auditStepList)
			auditStepDao.saveOrUpdate(auditStep);
		return true;
	}

	@Override
	@Transactional
	public boolean updateAuditStepForType(List<AuditStep> auditStepList)
	{
		if (auditStepList.isEmpty())
			return false;
		String typeCode = ((AuditStep) auditStepList.get(0)).getTypeCode();
		for (AuditStep auditStep : auditStepList)
		{
			auditStepDao
					.executeUpdate("delete from AuditStep a where a.typeCode='"
							+ typeCode + "' and a.step=" + auditStep.getStep());
		}
		for (AuditStep auditStep : auditStepList)
			auditStepDao.saveOrUpdate(auditStep);
		return true;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<AuditType> getAllType()
	{
		List<AuditType> list = new ArrayList<AuditType>();
		String[] typeCodes = AuditConstants.TYPE_CODES;
		String[] typeNames = AuditConstants.TYPE_NAMES;
		String[] typeServices = AuditConstants.TYPE_SERVICES;
		AuditType auditType;
		for (int i = 0; i < typeCodes.length && i < typeNames.length
				&& i < typeServices.length; i++)
		{
			auditType = new AuditType(typeCodes[i], typeNames[i],
					typeServices[i]);
			list.add(auditType);
		}
		return list;
	}

	public IAuditStepDao getAuditStepDao()
	{
		return auditStepDao;
	}

	public void setAuditStepDao(IAuditStepDao auditStepDao)
	{
		this.auditStepDao = auditStepDao;
	}

}
